package com.runda.partypolicy.app.base;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.runda.partypolicy.R;
import com.runda.partypolicy.app.ApplicationMine;
import com.runda.partypolicy.app.di.AppViewModelFactory;


import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.support.AndroidSupportInjection;



/**
 * author:  RD_YH
 * date:    2018/10/9
 * version: v1.0
 * description: 基础Fragment
 */

public abstract class BaseFragment<T extends BaseViewModel> extends Fragment {

    @Inject
    AppViewModelFactory viewModelFactory;

    private T viewModel;
    private Unbinder unBinder;

    private KProgressHUD waitingView;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidSupportInjection.inject(this);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayout(), null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unBinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.viewModel = initViewModel();
        initEvents();
        initNotSignEvent();
        initNoNetworkEvent();
        initShowMessageEvent();
        initStateLayoutEvent();
        initTokenOverTimeEvent();
        initShowOrDismissWaitingEvent();
        start();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unBinder != null) {
            unBinder.unbind();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ApplicationMine.getInstance().getRefWatcher().watch(this);
    }

    public KProgressHUD getWaitingView(boolean cancelable, String message) {
        hideWaitingView();
        waitingView = KProgressHUD.create(this.getContext())
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(cancelable)
                .setLabel(getString(R.string.pleaseWaiting))
                .setDetailsLabel(message)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f);
        return waitingView;
    }

    public void hideWaitingView() {
        if (waitingView != null && waitingView.isShowing()) {
            waitingView.dismiss();
        }
    }

//    public Alerter getNotSignedAlerter() {
//        return Alerter.create(_mActivity)
//                .setDuration(10000)
//                .enableSwipeToDismiss()
//                .setTitle(R.string.notSigned)
//                .setText(R.string.notSigned_content)
//                .setBackgroundColorRes(R.color.baseColor)
//                .setOnClickListener(view -> {
//                    Alerter.hide();
////                    ActivityUtils.startActivity(_mActivity, ActivityLogin.class);
//                });
//    }
//
//    public Alerter getNoNetworkAlerter() {
//        return Alerter.create(_mActivity)
//                .setDuration(10000)
//                .enableSwipeToDismiss()
//                .setTitle(R.string.noNetwork)
//                .setText(R.string.noNetwork_content)
//                .setBackgroundColorRes(R.color.baseColor)
//                .setOnClickListener(view -> {
//                    Alerter.hide();
//                    startActivity(IntentUtils.getLaunchAppDetailsSettingsIntent(
//                            AppUtils.getAppPackageName(), true));
//                });
//    }
//
//    public Alerter getTokenOvertimeAlerter() {
//        return Alerter.create(_mActivity)
//                .setDuration(10000)
//                .enableSwipeToDismiss()
//                .setTitle(R.string.tokenOvertime)
//                .setText(R.string.tokenOvertime_content)
//                .setBackgroundColorRes(R.color.baseColor)
//                .setOnClickListener(view -> {
//                    Alerter.hide();
////                    ActivityUtils.startActivity(_mActivity, ActivityLogin.class);
//                });
//    }

    public T getViewModel() {
        return viewModel;
    }

    public AppViewModelFactory getViewModelFactory() {
        return viewModelFactory;
    }

    public abstract int getLayout();

    public abstract T initViewModel();

    public abstract void initEvents();

    public abstract void start();

    public abstract void initNotSignEvent();

    public abstract void initNoNetworkEvent();

    public abstract void initShowMessageEvent();

    public abstract void initTokenOverTimeEvent();

    public abstract void initShowOrDismissWaitingEvent();

    public abstract void initStateLayoutEvent();
}
